package com.bjpowernode.money.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bjpowernode.money.constant.UserConstant;
import com.bjpowernode.money.model.FinanceAccount;
import com.bjpowernode.money.model.User;
import com.bjpowernode.money.service.FinanceAccountService;
import com.bjpowernode.money.service.UserService;
import com.bjpowernode.money.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author ZHAOZHENYU
 * @since 2022/10/20
 * 小金库控制层
 */
@Controller
public class MyCenterController {

    @Reference(interfaceClass = FinanceAccountService.class, version = "1.0.0", timeout = 20000)
    private FinanceAccountService financeAccountService;

    @Reference(interfaceClass = UserService.class,version = "1.0.0",timeout = 20000)
    private UserService userService;

    /**
     * 1、跳转到小金库
     * @return
     */
    @GetMapping("/loan/page/toMyCenter")
    public String toMyCenter(Model model, HttpServletRequest request){
        User user_session = (User) request.getSession().getAttribute(UserConstant.LOGIN_USER);
        FinanceAccount financeAccount = financeAccountService.queryFinanceByUserId(user_session.getId());
        model.addAttribute("financeAccount", financeAccount);
        return "myCenter";
    }

    /**
     * 2、小金库：头像上传
     * @param header
     * @param request
     * @return
     */
    @PostMapping("/loan/uploadHeader")
    @ResponseBody
    public Object uploadHeader(MultipartFile header,HttpServletRequest request){

        String originalFilename = header.getOriginalFilename();
        String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName= UUID.randomUUID().toString().replace("-", "") + fileType;
        // 放到static下的header文件夹
        File file = new File("D:\\IdeaProjects\\SH2207_S4_ZZY\\money\\005-money-web\\src\\main\\resources\\static\\header"
                + "/" + newFileName);
//        File file = new File(request.getServletContext().getRealPath("/header") + "/" + newFileName);
        try {
            header.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("上传存储失败");
        }
        // 取出session的userId
        User user_session = (User) request.getSession().getAttribute(UserConstant.LOGIN_USER);
        // 文件名存入数据库 单独更新 ，防止干扰其他数据
        int num = userService.updateHeaderById(newFileName,user_session.getId());
        // 将新文件名存入session
        if (num == 0){
            return Result.error("更新头像失败");
        }
        user_session.setHeaderImage(newFileName);
        request.getSession().setAttribute(UserConstant.LOGIN_USER, user_session);
        return Result.success();

    }


}
